package com.pitaya.avatar.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pitaya.avatar.domain.Area;
import com.pitaya.avatar.domain.School;
import com.pitaya.avatar.service.ISchoolService;

public class SchoolAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private ISchoolService schoolService;
	private School school;
	private List<School> schoolList;
	
	//Used for quick search school by key value
	//private Map<String,  ArrayList<School>> matchedSchools;
	private List<School> matchedSchools;
	private List<Area> houseAreas;
	private String key;
	private String city;
	private String schId;
	private String areaType;
	
	public void setKey(String k){
		this.key = k;
	}
	
	public String getKey(){
		return this.key;
	}
	
	public void setCity(String city){
		this.city = city;
	}
	
	public String getCity(){
		return this.city;
	}
	
	public void setMatchedSchools(List<School> schools){
		this.matchedSchools = schools;
	}
	
	public void setSchId(String id){
		this.schId = id;
	}
	
	public String getSchId(){
		return this.schId;
	}
	
	public void setAreaType(String type){
		this.areaType = type;
	}
	
	public String getAreaType(){
		return this.areaType;
	}
	
	public List<School> getMatchedSchools(){
		return this.matchedSchools;
	}
	
	public void setHouseAreas(List<Area> areas){
		this.houseAreas = areas;
	}
	
	public List<Area> getHouseAreas(){
		return this.houseAreas;
	}
	
	public List<School> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(List<School> schoolList) {
		this.schoolList = schoolList;
	}

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

	public void setSchoolService(ISchoolService schoolService) {
		this.schoolService = schoolService;
	}
	
	public ISchoolService getSchoolService() {
		return this.schoolService;
	}
	
	public String findSchool() {
		if(school == null) {
			school = new School();
		}
		
		if(school.getName().equals("")) {
			return "findFail";
		}
		Map<String,Object> session = ActionContext.getContext().getSession();
		List<School> list = new ArrayList<School>();
			

		//need add code
		
		list = schoolService.find(school);
		for (int i = 0; i < list.size(); i++) {
			School sch = list.get(i);
			System.out.println(sch.getSid());
			System.out.println(sch.getName());
			System.out.println(sch.getLongitude());
			System.out.println(sch.getLatitude());
		}
		
		session.put("schoolList", list);
		return "findSuccess";
	}
	
	public String addSchool() {
		Map<String,Object> session = ActionContext.getContext().getSession();
		school = (School)session.get("addSchool");
		if(schoolService.add(school)) {
			return "addSuccess"; 
		}else {
			return "addFailed";
		}
	}
	
	public String searchSchool(){
		matchedSchools = this.getSchoolService().searchSchools(city, key);
		return "searchSuccess";
	}
	
	public String getHousesAreaBySchool(){
		School school = new School();
		school.setPid(Long.valueOf(this.getSchId()));
		houseAreas = this.getSchoolService().getHousesByCluster(school, this.getAreaType());
		return "getHousesAreaSuccess";
	}
	
}
